package com.detrum.sportcamera.fragment;

import android.content.Context;
import android.media.midi.MidiManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import com.detrum.sportcamera.R;
import com.detrum.utils.LogUtils;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ExposureFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ExposureFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ExposureFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";

    // TODO: Rename and change types of parameters
    private int mParam1;

    private OnFragmentInteractionListener mListener;

    private SeekBar seeker;
    private int stopProgress;
    private int progress,value;
    private int flag = 1;
    private TextView auto;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment ExposureFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ExposureFragment newInstance() {
        ExposureFragment fragment = new ExposureFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_exposure, container, false);
        seeker = view.findViewById(R.id.seeker);
        auto = view.findViewById(R.id.auto);
        return view;
    }

    private void setStopProgress(){
        if(stopProgress>=0&&stopProgress<15){
            value = 13;
        }else if(stopProgress>=15&&stopProgress<45){
            value = 12;
        }else if(stopProgress>=45&&stopProgress<75){
            value = 11;
        }else if(stopProgress>=75&&stopProgress<105){
            value = 10;
        }else if(stopProgress>=105&&stopProgress<135){
            value = 9;
        }else if(stopProgress>=135&&stopProgress<165){
            value = 8;
        }else if(stopProgress>=165&&stopProgress<195){
            value = 7;
        }else if(stopProgress>=195&&stopProgress<225){
            value = 6;
        }else if(stopProgress>=225&&stopProgress<255){
            value = 5;
        }else if(stopProgress>=255&&stopProgress<285){
            value = 4;
        }else if(stopProgress>=285&&stopProgress<315){
            value = 3;
        }else if(stopProgress>=315&&stopProgress<345){
            value = 2;
        }else if(stopProgress>=345&&stopProgress<=360){
            value = 1;
        }
        mListener.selectItem(flag,value);
    }

    @Override
    public void onResume() {
        super.onResume();
        setProgress(mParam1);
        auto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.selectItem(flag,0);
            }
        });
        seeker.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                stopProgress = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                setStopProgress();
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void setProgress(int mParam1){
        LogUtils.e("ExposureFragment setProgress = "+mParam1);
        if(mParam1==0){
            progress = 180;
        }else if(mParam1==1){
            progress = 360;
        }else if(mParam1==2){
            progress = 330;
        }else if(mParam1==3){
            progress = 300;
        }else if(mParam1==4){
            progress = 270;
        }else if(mParam1==5){
            progress = 240;
        }else if(mParam1==6){
            progress = 210;
        }else if(mParam1==7){
            progress = 180;
        }else if(mParam1==8){
            progress = 150;
        }else if(mParam1==9){
            progress = 120;
        }else if(mParam1==10){
            progress = 90;
        }else if(mParam1==11){
            progress = 60;
        }else if(mParam1==12){
            progress = 30;
        }else if(mParam1==13){
            progress = 0;
        }
        seeker.setProgress(progress);
    }

    public void setmParam1(int mParam1){
        this.mParam1 = mParam1;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
        void selectItem(int flag,int num);
    }
}
